// 2013-7-29下午02:48:59  net.betou.trade.admin.action.CategoryAct.java

package net.betou.trade.admin.action;

import static net.betou.common.page.SimplePage.cpn;
import static net.betou.common.page.SimplePage.cpnPageSize;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.betou.common.page.Pagination;
import net.betou.common.util.TradeUtils;
import net.betou.trade.action.abs.ActionSupport;
import net.betou.trade.entity.main.AgentAndAdmins;
import net.betou.trade.entity.main.News;
import net.betou.trade.main.manager.NewsMng;
import net.betou.trade.web.Messages;
import net.betou.trade.web.ResponseUtil;
import net.betou.trade.web.WebErrors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 新闻
 * @author 叶装装
 */
@Controller
public class NewsAct extends ActionSupport< News >{
	
	private static final String DIRECTORY = "news";
	private static final Logger log = LoggerFactory.getLogger( NewsAct.class );
	
	@RequestMapping( "/news/v_list.jspx" )
	public String list(  Integer pageSize, Integer pageNo, String searchProperty, String searchValue, String orderProperty, String orderDirection, HttpServletRequest request, HttpServletResponse response, ModelMap model  ){
		
		pageSize = cpnPageSize( pageSize );
		pageNo = cpn( pageNo );
		
		AgentAndAdmins admin = TradeUtils.getAdmin( request ); // 获取当前管理员
		Pagination pagination = manager.getPage( admin, searchProperty, searchValue, orderProperty, orderDirection, pageNo, pageSize );
		
		model.addAttribute( "admin", admin );
		paramToModel( model, pagination, null, pageSize, searchProperty, searchValue, orderProperty, orderDirection, null );
		
		return getListPage( );
	}
	
	@RequestMapping( "/news/v_add.jspx" )
	public String v_add( HttpServletRequest request, ModelMap model ){
		
		WebErrors errors = validateToSave( request );
		
		if( errors.hasErrors( ) ){
			return errors.showErrorPage( model );
		}
		
		return getAddPage( );
	}
	
	@RequestMapping( "/news/o_save.jspx" )
	public String o_save( News bean,  HttpServletRequest request, HttpServletResponse response, ModelMap model ){
		
		WebErrors errors = validateSave( bean );
		
		if( errors.hasErrors( ) ){
			return errors.showErrorPage( model );
		}
		
		AgentAndAdmins admin = TradeUtils.getAdmin( request ); // 获取当前管理员
		
		bean = manager.save( bean, admin );
		
		logMng.operating( 0, request, Messages.LOG_NEWS, "id=" + bean.getId( ) + ",title=" + bean.getTitle( )  );
		
		return "redirect:v_list.jspx";
	}
	
	@RequestMapping ( "/news/v_edit.jspx" )
	public String v_edit( Integer id, HttpServletRequest request, ModelMap model ){

		WebErrors errors = validateUpdate( request, id );
		
		if( errors.hasErrors( ) ){
			return errors.showErrorPage( model );
		}
		
		News bean = manager.findById( id );
		
		model.addAttribute( "bean", bean );
		
		return getUpdatePage( );
	}
	
	@RequestMapping ( "/news/o_update.jspx" )
	public String o_update(  Integer id, News bean, HttpServletRequest request, ModelMap model ){
		
		AgentAndAdmins admin = TradeUtils.getAdmin( request ); // 获取当前管理员
		
		WebErrors errors = validateOUpdate( request, id, admin.getId( ), admin.getSystem( ) );
		
		if( errors.hasErrors( ) ){
			return errors.showErrorPage( model );
		}
		
		bean = manager.update( bean );
		
		logMng.operating( 1, request, Messages.LOG_NEWS, "id=" + bean.getId( ) + ",title=" + bean.getTitle( ) );
		
		return "redirect:v_list.jspx";
	}
	
	
	@RequestMapping( "/news/o_delete.jspx" )
	public void o_delete( Integer [ ] ids, HttpServletResponse response, HttpServletRequest request ){
		
		AgentAndAdmins admin = TradeUtils.getAdmin( request ); // 获取当前管理员
		
		ResponseUtil util = validateDelete( ids, admin.getId( ), admin.getSystem( ), response, request );

		if( util.hasResponse( ) ){
			util.renderJson( );
			return;
		}

		News [ ] beans = manager.deleteByIds( ids );

		for ( News bean : beans ){
			log.info( "delete News id={}", bean.getId( ) );
			logMng.operating( 2, request, Messages.LOG_NEWS, "id=" + bean.getId( ) + ",title=" + bean.getTitle( ) );
		}

		util.addSuccessResponse( Messages.getDeleteSuccessMessage( beans.length ) );

		util.renderJson( );
		return;
		
	}
	
	private ResponseUtil validateDelete( Integer [ ] ids, Integer selfId, Boolean system, HttpServletResponse response, HttpServletRequest request ) {
		ResponseUtil util = ResponseUtil.create( response );

		if( ids == null || ids.length == 0 ){
			util.addErrorResponse( Messages.DATA_NOT_SELECT );
			return util;
		}

		for ( Integer id : ids ){
			News bean = manager.findMyNews( id, selfId, system );
			
			if( bean == null ){
				util.addErrorResponse(  system ? Messages.DATA_NOT_EXIST : Messages.DATA_NOT_OPERATING   );
				break;
			}
		}
		return util;
	}
	
	
	private WebErrors validateOUpdate( HttpServletRequest request, Integer id, Integer selfId, Boolean system ){
		WebErrors errors = WebErrors.create( );
		
		if( id == null || id == 0 ){
			errors.addError( Messages.DATA_NOT_EXIST );
			return errors;
		}
		
		News bean = manager.findMyNews( id, selfId, system );
		
		if( bean == null ){
			errors.addError( system ? Messages.DATA_NOT_EXIST : Messages.DATA_NOT_OPERATING );
			return errors;
		}
		
		return errors;
			
	}
	
	private WebErrors validateUpdate( HttpServletRequest request, Integer id ){
		WebErrors errors = WebErrors.create( );
		
		if( id == null || id == 0 ){
			errors.addError( Messages.DATA_NOT_EXIST );
			return errors;
		}
		
		News bean = manager.findById( id );
		
		if( bean == null ){
			errors.addError( Messages.DATA_NOT_EXIST );
			return errors;
		}
		
		return errors;
			
	}
	
	private WebErrors validateToSave( HttpServletRequest request ){
		WebErrors errors = WebErrors.create( );
		AgentAndAdmins admin = TradeUtils.getAdmin( request );
		
		if( admin.getSystem( ) ){
			errors.addError( Messages.NEWS_ADMIN_SYSTEM_MANAGER_NOT_TO_ADD );
		}
		return errors;
	}
	
	private WebErrors validateSave( News bean ){
		WebErrors errors = WebErrors.create( );
		
		return errors;
	}
	
	@Override
	protected String getDirectory( ) {
		return DIRECTORY;
	}
	
	@Autowired
	private NewsMng manager;

}
